<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="父冷库" prop="parentId">
        <el-select v-model="queryParams.parentId" placeholder="请选择所属冷库">
          <el-option
            v-for="cold in parentCold"
            :key="cold.coldId"
            :label="cold.coldName"
            :value="cold.coldId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区域名称" prop="subName">
        <el-input
          v-model="queryParams.subName"
          placeholder="请输入区域名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入区域描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小容积" prop="capacity1">
        <el-input-number
          v-model="queryParams.capacity1"
          placeholder="最小容积"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="最大容积" prop="capacity2">
        <el-input-number
          v-model="queryParams.capacity2"
          placeholder="最大容积"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="冷库类型" prop="coldId">
        <el-select v-model="queryParams.coldId" placeholder="请选择所属冷库" @change="onColdTypeChanged">
          <el-option
            v-for="cold in coldType"
            :key="cold.coldTypeId"
            :label="cold.typeName"
            :value="cold.coldTypeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最小价格" prop="dailyPrice1">
        <el-input-number
          v-model="queryParams.dailyPrice1"
          placeholder="最小价格"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="最大价格" prop="dailyPrice2">
        <el-input-number
          v-model="queryParams.dailyPrice2"
          placeholder="最大价格"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="负责人" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder="请输入负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
          <el-option
            v-for="dict in dict.type.lg_sub_cold_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" width="80">
        <template slot-scope="scope">
          {{ (scope.$index + 1) + (queryParams.pageNum - 1) * queryParams.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="冷库名称" align="center" prop="coldName">
        <template slot-scope="scope">
          <el-button type="text" @click="getMap(scope.row)">{{ scope.row.coldName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="区域名称" align="center" prop="subName" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="容积" align="center" prop="capacity">
        <template slot-scope="scope">
          {{ scope.row.capacity }} 立方米
        </template>
      </el-table-column>
      <el-table-column label="区域类型" align="center" prop="typeName" />
      <el-table-column label="负责人" align="center" prop="userName" />
      <el-table-column label="当前所属" align="center" prop="currentOwner" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_sub_cold_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"  v-hasPermi="['equipment:temperature:updateVehicle']" @click="submitForm(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getEvery"
    />

    <el-dialog :title="title" :visible.sync="open2" width="1000px"  append-to-body>
      <baidu-map
        class="map2"
        :center="{ lng: mapX, lat: mapY }"
        :zoom="15"
        :scroll-wheel-zoom="true"
        mapType="BMAP_NORMAL_MAP"
        @click="handleMapClick"
      >
        <!-- 添加地图控件 -->
        <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
        <bm-scale anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-scale>
        <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>

        <!-- 添加定位控件 -->
        <bm-geolocation
          anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
          :showAddressBar="true"
          :autoLocation="true"
        ></bm-geolocation>

        <!-- 添加路况图层 -->
        <bm-traffic :type="0"></bm-traffic>

        <!-- 添加卫星图层（可选） -->
        <bm-overview-map anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-overview-map>
      </baidu-map>
      <div slot="footer" class="dialog-footer" style="padding-left: 330px;padding-right: 0">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAll,getParentCold,getColdType,getManagerByColdName,getAllCount, listPost } from "@/api/storage/smallCold";
import {getBoundCold} from "@/api/equipment/coldMachine";
// import {
//   updateSmallCold,
//   listSmallCold
// } from "@/api/equipment/camera";
import {updateSmallCold} from "@/api/equipment/coldMachine";
import store from "@/store";
export default {
  name: "Post",
  props: {
    mid: {
      type: [Number, String], // 根据实际情况调整类型
      default: null
    }
  },
  dicts: ['sys_normal_disable','lg_sub_cold_status'],
  data() {
    return {
      userId:store.state.user.id, //将缓存的用户id赋值给userId
      userName:store.state.user.name, //将缓存的用户id赋值给userId
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 父冷库id与名字信息
      parentCold: [],
      //冷库类型与名字信息
      coldType: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示弹出层2
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
        subName: undefined,
        description: undefined,
        capacity1: undefined,
        capacity2: undefined,
        dailyPrice1: undefined,
        dailyPrice2: undefined,
        coldId: undefined,
        people: undefined,
        createBy: undefined,
        updateBy: undefined,
        beginTime: undefined,
        endTime: undefined,
        status: undefined,
      },
      coldUserName: "",
    };
  },
  created() {
    this.getList();
    this.getEvery();
    //获得父冷库一些信息，方便制作下拉框
    this.getColdStorge();
    //获得冷库类型的一些相关信息，方便制作下拉框
    this.getColdType();
  },
  methods: {
    //点击冷库名字弹出地图
    getMap(some){
      this.open2 = true;
      this.mapX = some.coordinateX;
      this.mapY = some.coordinateY;
      console.log(this.mapX);
    },
    /** 查询数据 */
    getEvery(){
      this.loading = true;
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      getBoundCold(this.queryParams).then(response => {
        console.log(response.data);
        this.postList = response.data;
        this.getTotalSome();
        this.loading = false;
      });
    },
    /** 获得数据条数 */
    getTotalSome(){
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      getAllCount(this.queryParams).then(response => {
        this.total = response.data;
      });
    },
    /** 获得父冷库信息 */
    getColdStorge(){
      getParentCold().then(response => {
        this.parentCold = response.data;
      });
    },
    /** 获得冷库类型信息 */
    getColdType(){
      getColdType().then(response => {
        this.coldType = response.data;
      });
    },
    /** 给区域类型赋值 */
    onColdTypeChanged(){
      for (var i = 0; i < this.coldType.length; i++){
        if (this.coldType[i].coldTypeId == this.form.cold_id){
          this.form.coldId = this.coldType[i].coldTypeId;
          alert(this.form.coldId);
          break;
        }
      }
    },
    /** 获取父冷库名字 */
    onParentColdChanged(){
      var parentId = this.form.parentId;
      // alert(parentId);
      for (var i = 0; i < this.parentCold.length; i++){
        if (this.parentCold[i].coldId == parentId){
          this.form.coldName = this.parentCold[i].coldName;
          this.getManagerByColdNameSome();
          break;
        }
      }
    },
    /** 获取冷库管理员名字 */
    getManagerByColdNameSome(){
      getManagerByColdName(this.form.coldName).then(response => {
        console.log(response.data);
        this.coldUserName = response.data.userName;
        this.form.managerId = response.data.userId;
        // alert(this.form.managerId);
      });
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        // this.postList = response.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open2 = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getEvery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 将 submitForm 方法移动到 methods 对象内部
    submitForm(row) {
      updateSmallCold({
        mid: this.mid,
        subId: row.subId,
        updateBy: this.$store.state.user.name,
      }).then(() => {
        console.log(row.subId);
        this.$message.success('绑定成功');
        this.$emit('selected', row.subId);
        // this.$emit('close'); // 关键代码：触发自定义事件
      }).catch(() => {
        this.$message.error('绑定失败');
      });
    }
  },
};
</script>
<style>
.map2{
  width: 100%;
  height: 500px;
}
</style>
